package com.xbongbong.paas.service.impl;

import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataDeleteBatchDTO;
import com.xbongbong.paas.pojo.dto.FormDataThoroughDeleteDTO;
import com.xbongbong.paas.pojo.vo.FormDataAddVO;
import com.xbongbong.paas.pojo.vo.FormDataDeleteBatchVO;
import com.xbongbong.paas.pojo.vo.FormDataThoroughDeleteVO;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.service.UserService;
import com.xbongbong.paas.service.WorkflowOperatorDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.ListBatchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author 吴峰
 * @date 2021/07/05 10:26
 */
@Service("workflowOperatorService")
public class WorkflowOperatorDataServiceImpl implements WorkflowOperatorDataService {

    private static final Logger LOGGER = LoggerFactory.getLogger(WorkflowOperatorDataServiceImpl.class);

    @Resource
    private UserService userService;
    @Resource
    private PaasFormDataService paasFormDataService;
    @Resource
    private ListBatchService listBatchService;

    @Override
    public FormDataAddVO add(FormDataAddDTO formDataAddDTO) throws XbbException {
        try {
           UserVO userVO = userService.getUserVO(formDataAddDTO.getCorpid(), formDataAddDTO.getUserId(), true ,true);
           formDataAddDTO.setLoginUser(userVO);
           formDataAddDTO.setLoginUserName(userVO.getName());
           return paasFormDataService.add(formDataAddDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowOperatorDataServiceImpl.add error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public FormDataDeleteBatchVO delete(FormDataDeleteBatchDTO formDataDeleteBatchDTO) throws XbbException {
        try {
            UserVO userVO = userService.getUserVO(formDataDeleteBatchDTO.getCorpid(), formDataDeleteBatchDTO.getUserId(), true ,true);
            formDataDeleteBatchDTO.setLoginUser(userVO);
            formDataDeleteBatchDTO.setLoginUserName(userVO.getName());
            formDataDeleteBatchDTO.setIsSkipPermissionCheck(BasicConstant.ONE);
            return listBatchService.deleteBatch(formDataDeleteBatchDTO);
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowOperatorDataServiceImpl.add error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public FormDataThoroughDeleteVO thoroughDelete(FormDataThoroughDeleteDTO formDataThoroughDeleteDTO) throws XbbException {
        try {
            UserVO userVO = userService.getUserVO(formDataThoroughDeleteDTO.getCorpid(), formDataThoroughDeleteDTO.getUserId(), true ,true);
            formDataThoroughDeleteDTO.setLoginUser(userVO);
            formDataThoroughDeleteDTO.setLoginUserName(userVO.getName());
            if (Objects.equals(XbbRefTypeEnum.CLUE.getCode(), formDataThoroughDeleteDTO.getBusinessType()) || Objects.equals(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), formDataThoroughDeleteDTO.getBusinessType())) {
                return listBatchService.thoroughDelete(formDataThoroughDeleteDTO);
            } else if (Objects.equals(SaasMarkEnum.PAAS.getCode(), formDataThoroughDeleteDTO.getSaasMark())) {
                return listBatchService.thoroughDelete(formDataThoroughDeleteDTO);
            }
            return new FormDataThoroughDeleteVO();
        } catch (XbbException e) {
            throw e;
        } catch (Exception e) {
            LOGGER.error("WorkflowOperatorDataServiceImpl.add error", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
